package com.redoor.app.stake.auth.service.impl;

import com.redoor.app.stake.auth.dao.BaseMenuMapper;
import com.redoor.app.stake.auth.model.BaseMenu;
import com.redoor.app.stake.auth.model.constants.ManagerRedisConstants;
import com.redoor.app.stake.auth.service.BaseMenuRedisService;
import com.redoornetwork.framework.data.redis.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created by DELL on 2018/4/12.
 */

@Service
public class BaseMenuRedisServiceImpl implements BaseMenuRedisService {

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public boolean insertMenuToRedis(List<BaseMenu> list) {
        if(list == null ){
            return false;
        }
        try {
            for (BaseMenu menu : list){
                String parentUuid = menu.getParentuuid();
                if(!StringUtils.isEmpty(parentUuid)){
                    redisUtils.hset(ManagerRedisConstants.REDIS_BUTTEN,parentUuid.toUpperCase(), menu);
                }
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public String getBaseMenuFromRedis(String parentUuid) {
        if(StringUtils.isEmpty(parentUuid) || !redisUtils.hHasKey(ManagerRedisConstants.REDIS_BUTTEN,parentUuid.toUpperCase())){
            return null;
        }
        return redisUtils.hget(ManagerRedisConstants.REDIS_BUTTEN,parentUuid.toUpperCase()).toString();
    }
}
